Plotting basics

OpenGHG provides some simple plotting functionality to make it easy to plot the data you retrieve from the object store. To do that we use Plotly which makes nice looking, responsive graphs easily. We also show how you can access the raw data that can be used with your favourite plotting library.

As in the previous tutorial we’ll start by setting up a temporary object store.

import os
import tempfile

from openghg.client import process_files, search
from openghg.util import retrieve_example_data

tmp_dir = tempfile.TemporaryDirectory()
os.environ["OPENGHG_PATH"] = tmp_dir.name   # temporary directory

Find some data

First we need to retrieve some data to plot. Here we download some data from our example data repository and then process it for storage in the object store.

bsd_datafile = retrieve_example_data(path="timeseries/bsd_example.tar.gz")

decc_results = process_files(files=bsd_datafile, data_type="CRDS", site="bsd", network="DECC")
  0%|          | 0/1 [00:00<?, ?it/s]
Processing: bsd.picarro.hourly.42m.dat:   0%|          | 0/1 [00:00<?, ?it/s]
Processing: bsd.picarro.hourly.42m.dat: 100%|██████████| 1/1 [00:01<00:00,  1.22s/it]
Processing: bsd.picarro.hourly.42m.dat: 100%|██████████| 1/1 [00:01<00:00,  1.22s/it]

Now we can retrieve some data and make a quick plot

search_results = search(site="bsd", species="co2")
search_results
Site: BSD
---------
co2 at 42m
bsd_co2_data = search_results.retrieve(site="bsd", species="co2")

Have a quick look at the ObsData object that gets returned by the retrieve function.

bsd_co2_data
ObsData(data=<xarray.Dataset>
Dimensions:                     (time: 53033)
Coordinates:
  * time                        (time) datetime64[ns] 2014-01-30T11:40:44 ......
Data variables:
    co2                         (time) float64 409.6 409.4 411.2 ... 408.9 409.3
    co2_variability             (time) float64 0.036 0.064 0.114 ... 0.756 0.193
    co2_number_of_observations  (time) float64 449.0 452.0 454.0 ... 178.0 200.0
Attributes: (12/25)
    data_owner:            Simon O'Doherty
    data_owner_email:      s.odoherty@bristol.ac.uk
    inlet_height_magl:     42m
    comment:               Cavity ring-down measurements. Output from GCWerks
    long_name:             bilsdale
    site:                  bsd
    ...                    ...
    calibration_scale:     WMO-X2007
    sampling_period_unit:  s
    station_longitude:     -1.15033
    station_latitude:      54.35858
    station_long_name:     Bilsdale, UK
    station_height_masl:   380.0, metadata={'site': 'bsd', 'instrument': 'picarro', 'sampling_period': '3600', 'inlet': '42m', 'port': '10', 'type': 'air', 'network': 'decc', 'species': 'co2', 'scale': 'wmo-x2007', 'long_name': 'bilsdale', 'calibration_scale': 'wmo-x2007', 'data_owner': "simon o'doherty", 'data_owner_email': 's.odoherty@bristol.ac.uk', 'station_longitude': -1.15033, 'station_latitude': 54.35858, 'station_long_name': 'bilsdale, uk', 'station_height_masl': 380.0, 'inlet_height_magl': '42m', 'data_type': 'timeseries'})

Now we create a quick plot using the plot_timeseries member of ObsData. You can pass any of title, xlabel, ylabel and units to this function.

Note: the plot below may not show up on the online documentation version of this notebook.

bsd_co2_data.plot_timeseries(units="ppm")

Clean up

If you used the tmp_dir as a location for your object store at the start of the tutorial you can run the cell below to remove any files that were created to make sure any persistant data is refreshed when the notebook is re-run.

tmp_dir.cleanup()